// pages/judge/judge.js
Page({
  data: {
    list:{},
    finishtime:'2022-5-25 9:30',
    phone:'12302541031',
    

    nice_url : '/static/image/笑脸.png',
    common_url :'/static/image/平静 (1).png',
    worried_url : '/static/image/哭脸.png',
    flag_url:null,

    judge_text:'',
    tempFilePaths: []
    
  },

  onLoad:function(options){
    console.log(JSON.parse(options.list))
    this.setData({
      list:JSON.parse(options.list)
    })
    
  },

  changetonice:function(){
    this.setData({
      nice_url:'/static/image/笑脸后.png',
      common_url :'/static/image/平静 (1).png',
      worried_url : '/static/image/哭脸.png',
      flag_url:1
    })
  },
  changetocommon:function(){
    this.setData({
      nice_url:'/static/image/笑脸.png',
      common_url :'/static/image/平静后.png',
      worried_url : '/static/image/哭脸.png',
      flag_url:2
    })
  },
  changetoworried:function(){
    this.setData({
      nice_url:'/static/image/笑脸.png',
      common_url :'/static/image/平静.png',
      worried_url : '/static/image/哭脸后.png',
      flag_url:3
    })
  },

  judgetext:function(e){
    this.setData({
      judge_text:e.detail.value
    })
  },

  // 上传图片方法
  
  upload: function () {
    let that = this;
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: res => {
        wx.showToast({
          title: '正在上传...',
          icon: 'loading',
          mask: true,
          duration: 1000
        })
        console.log(res)
        console.log(res.tempFilePaths)

          this.setData({
            tempFilePaths:this.data.tempFilePaths.concat(res.tempFilePaths)
          })
    
        //上传完成后把文件上传到服务器
        var count = 0;
        for (var i = 0, h = this.data.tempFilePaths.length; i < h; i++) {
          //上传文件
          /*  wx.uploadFile({
              url: HOST + '地址路径',
              filePath: tempFilePaths[i],
              name: 'uploadfile_ant',
              header: {
                "Content-Type": "multipart/form-data"
              },
              success: function (res) {
                count++;
                //如果是最后一张,则隐藏等待中  
                if (count == tempFilePaths.length) {
                  wx.hideToast();
                }
              },
              fail: function (res) {
                wx.hideToast();
                wx.showModal({
                  title: '错误提示',
                  content: '上传图片失败',
                  showCancel: false,
                  success: function (res) { }
                })
              }
            });*/
        }

      }
    })
  },
  /*预览图片方法*/
  listenerButtonPreviewImage: function (e) {
    let index = e.target.dataset.index;
    console.log(index)
    let that = this;
    console.log(that.data.tempFilePaths[index]);
    console.log(that.data.tempFilePaths);
    wx.previewImage({
      current: that.data.tempFilePaths[index],
      urls: that.data.tempFilePaths,
      //这根本就不走
      success: function (res) {
        //console.log(res);
      },
      //也根本不走
      fail: function () {
        //console.log('fail')
      }
    })
  },
  /* 长按删除图片*/
  deleteImage: function (e) {
    var that = this;
    var tempFilePaths = that.data.tempFilePaths;
    var index = e.currentTarget.dataset.index;//获取当前长按图片下标
    wx.showModal({
      title: '提示',
      content: '确定要删除此图片吗？',
      success: function (res) {
        if (res.confirm) {
          console.log('确定');
          tempFilePaths.splice(index, 1);
        } else if (res.cancel) {
          console.log('取消');
          return false;
        }
        that.setData({
          tempFilePaths
        });
      }
    })
  },

  submit_judge:function(){

    let pages = getCurrentPages(); // 当前页的数据，
    let prevPage = pages[pages.length - 2]; // 上一页的数据

    if(this.data.flag_url == null){
      wx.showToast({
        icon:'error',
        title: '请选择评价等级',
      })
    }else{
        wx.showModal({
      title: '',
      content: '是否提交评价',
      success:res=>{

        let level = this.data.flag_url;
        if(level == 1){
          var judge_level = '优';
          this.data.list.coin = parseInt(this.data.list.coin)*2
        }else if(level == 2){
            var judge_level ='良'
        }else{
          var judge_level = '差'
          this.data.list.coin = parseInt(this.data.list.coin)/2
          this.data.list.hour = parseInt(this.data.list.hour)/2
        }
        this.setData({
          list:this.data.list
        })
        console.log(this.data.list)
        console.log(judge_level)
        console.log(this.data.judge_text)
        console.log(this.data.tempFilePaths)

        var arrt = {
          serid:this.data.list.serid,
          judgelevel:judge_level,
          judgetext:this.data.judge_text,
          judgepic:JSON.stringify(this.data.tempFilePaths)
        }
        wx.request({
          url: 'http://127.0.0.1:8082/demo1/superadmin/updateserjudge',
          data:JSON.stringify(arrt),
          method:'POST',
          success:(res)=>{
            console.log(res)
          }
        })
       
        var arrvol = {
          volid : this.data.list.servolid,
          volhour : this.data.list.hour,
          coin : this.data.list.coin
        }
        
        wx.request({
          url: 'http://127.0.0.1:8082/demo1/superadmin/updatevolunteertime',
          data:JSON.stringify(arrvol),
          method:'POST',
          success:(res)=>{
            console.log(res)
            prevPage.onLoad()
            wx.navigateBack({
              delta: 1
            });
          }
        })
      
      
      }
   })
    }
  }

})